package com.qixiaobao.web.controller.message;

import com.qixiaobao.common.core.domain.AjaxResult;
import com.qixiaobao.common.core.domain.model.LoginUser;
import com.qixiaobao.common.core.page.TableDataInfo;
import com.qixiaobao.common.exception.UtilException;
import com.qixiaobao.system.domain.dto.MessageDTO;
import com.qixiaobao.system.service.IMessageService;
import com.qixiaobao.web.controller.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 站内信
 * @Author yangxm
 * @Date 2025/7/10 15:37
 */
@RestController
@RequestMapping("/message")
public class MessageController extends BaseController {

    @Autowired
    private IMessageService messageService;


    /**
     * @Description 站内信列表
     * @Author yangxm
     * @Date 2025/7/10 16:06
     * @param messageDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping
    public TableDataInfo list(MessageDTO messageDTO){
        LoginUser user = getLoginUser();
        if (user ==  null){
            throw new UtilException("获取登录信息失败，请重新登录！");
        }
        messageDTO.setUserId(user.getId());
        startPage();
        return getDataTable(messageService.selectMessageList(messageDTO));
    }

    /**
     * @Description 站内信阅读
     * @Author yangxm
     * @Date 2025/7/10 16:06
     * @param messageUserId
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @PostMapping("/read")
    public AjaxResult read(Long messageUserId){
        return toAjax(messageService.markAsRead(messageUserId));
    }

    /**
     * @Description 站内信删除
     * @Author yangxm
     * @Date 2025/7/10 16:06
     * @param messageUserId
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @DeleteMapping("/{messageUserId}")
    public AjaxResult delete(@PathVariable Long messageUserId){
        return toAjax(messageService.deleteMessageUser(messageUserId));
    }

    /**
     * @Description 批量阅读
     * @Author yangxm
     * @Date 2025/7/14 14:51
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @PostMapping("/readAll")
    public AjaxResult readAll(@RequestBody MessageDTO messageDTO){
        return toAjax(messageService.markAsReadAll(messageDTO));
    }

    /**
     * @Description 批量删除
     * @Author yangxm
     * @Date 2025/7/14 14:51
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @PostMapping("/deleteAll")
    public AjaxResult deleteAll(@RequestBody MessageDTO messageDTO){
        return toAjax(messageService.deleteAll(messageDTO));
    }
}
